﻿using Microsoft.IdentityModel.Tokens;
using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore.Helper;
using System.Windows.Input;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class UserGroupEditViewModel : DialogViewModelBase, IDialogAware
    {
        private UserGroup Group { get; set; }
        private DelegateCommand<object> _okDialogCommand;
        private IOAuthService _authService;
        public UserGroupEditViewModel(IDialogService dialogService, IOAuthService oAuthService) : base(dialogService)
        {
            Title = "编辑用户组";
            _authService = oAuthService;
        }
        public override async void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            UserGroupId = parameters.GetValue<string>("groupId");
            if (!UserGroupId.IsNullOrEmpty())
                Group = await _authService.GetUserGroupById(UserGroupId);
            if (Group != null)
            {
                UserGroupName = Group.UserGroupName;
                GroupNote = Group.GroupNote;
            }
            else
            {
                Group = new UserGroup();
            }
        }
        private string _userGroupId;
        public string UserGroupId
        {
            get { return _userGroupId; }
            set { SetProperty(ref _userGroupId, value); }
        }

        private string _inUserName;
        public string UserGroupName
        {
            get { return _inUserName; }
            set { SetProperty(ref _inUserName, value); }
        }

        private string _groupNote;
        public string GroupNote
        {
            get { return _groupNote; }
            set { SetProperty(ref _groupNote, value); }
        }

        public ICommand OkDialogCommand
        {
            get
            {
                if (_okDialogCommand != null) return _okDialogCommand;
                _okDialogCommand = new DelegateCommand<object>(onOkDialogCommand);
                return _okDialogCommand;
            }
        }

        private async void onOkDialogCommand(object parameter)
        {
            if (UserGroupName.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您尚未设置用户组名。");
                return;
            }

            try
            {
                Group.UserGroupName = UserGroupName;
                Group.GroupNote = GroupNote;

                bool? r = false;
                if (UserGroupId.IsNullOrEmpty())
                {
                    r = await _authService.CreateUserGroupr(Group);
                }
                else
                {
                    r = await _authService.UpdateUserGroupr(Group);
                }
                if (r.HasValue && r.Value)
                {
                    Dialog.ShowAltert("编辑用户组信息成功！！！");
                    RaiseRequestClose(new DialogResult(ButtonResult.OK));
                }
                else
                {
                    RaiseRequestClose(new DialogResult(ButtonResult.None));
                }
            }
            catch (Exception x)
            {
                var xMessage = $"编辑用户组信息失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
